package view.ui
{
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.events.Event;
	import flash.filesystem.File;
	import flash.geom.Rectangle;
	
	import events.MyEvent;
	
	import loader.SinglePicLoader;
	
	import utils.CopyToImageFile;
	import utils.MyTimer;

	
	public class CutOutWorker extends ProgressView
	{
		private var source:File;
		private var targetFolder:File;
		public var index:uint=0;
		private var singlePicLoader:SinglePicLoader;
		private var sourceBM:Bitmap;
		private var imageSaver:CopyToImageFile;
		private var analyzeTimer:MyTimer;
		private var analyzeTime:Number=0.04;
		private var x_pos:Number=0;
		private var y_pos:Number=0;
		private var nowAnalyzePosition:uint=0;
		private var source_width:Number;
		private var source_height:Number;
		private var area:Number;
		private var rate:Number=0;
		private var left:Number=NaN;
		private var right:Number=NaN;
		private var top:Number=NaN;
		private var bottom:Number=NaN;
		private var stop:Boolean=false;
		private var fileNameTxt:FileNameTextField;
		
		
		private var left_x_pos:Number=0;
		private var left_y_pos:Number=0;
		
		private var right_x_pos:Number=0;
		private var right_y_pos:Number=0;
		
		private var top_x_pos:Number=0;
		private var top_y_pos:Number=0;
		
		private var bottom_x_pos:Number=0;
		private var bottom_y_pos:Number=0;
		
		private var left_percent:Number=0;
		private var right_percent:Number=0;
		private var top_percent:Number=0;
		private var bottom_percent:Number=0;
		
		private var leftChecked:Number=NaN;
		private var rightChecked:Number=NaN;
		private var topChecked:Number=NaN;
		private var bottomChecked:Number=NaN;
		
		public function CutOutWorker(s:File,t:File)
		{
			super();
			source=s;
			targetFolder=t;
		}
		
		public function stopWork():void
		{
			stop=true;
			if(analyzeTimer)
			{
				analyzeTimer.GC();
				analyzeTimer=null;
			}
		}
		
		private function addFileNameTxt():void
		{
			if(!fileNameTxt)
			{
				fileNameTxt=new FileNameTextField();
				var filename:String=source.name;
				fileNameTxt.text=filename.substr(0,filename.length-4);
				//trace("fileNameTxt"+fileNameTxt);
				
				this.addChild(fileNameTxt);
				fileNameTxt.x=10;
				fileNameTxt.y=20;
			}
			
		}
		
		public function wait():void
		{
			this.text="等待";
			progress(1);
			addFileNameTxt();
		}
		
		public function startWork():void
		{
			addFileNameTxt();		
			this.text="加载图片";
			trace("source.url="+source.url);
			singlePicLoader=new SinglePicLoader(source.url,handlePicLoaded);
		}
		
		private function handlePicLoaded(bm:Bitmap):void
		{
			//trace("到达加载图片阶段");
			singlePicLoader=null;
			sourceBM=bm;
			var bmpd:BitmapData=sourceBM.bitmapData;
			this.progress(5);
			this.text="正在打开图片";
			
			try
			{
				source_width=bmpd.width;
				source_height=bmpd.height;
			}
			catch(e:Error)
			{
				this.dispatchEvent(new MyEvent("fileError",this.index));
				this.red();
				this.text=e.message;
				return;
			}
			analyzeTimer=new MyTimer(analyze,analyzeTime,1);
			analyzeTimer.start();
			
			
		}
	
		private function analyze():void
		{
			if(stop)
			{
				analyzeTimer.GC();
				analyzeTimer=null;
				return;
			}
			/**
			 * 开始获取图片数据
			 */
			/*var bitmapData:BitmapData = new BitmapData(sourceBM.width,sourceBM.height);
			bitmapData.draw(sourceBM);*/
			
			if(!imageSaver)
			{
			imageSaver=new CopyToImageFile();
			imageSaver.addEventListener("imageSaved",handlePngSaved);
			}
			this.progress(50);
			this.text="开始写数据文件";			
			var width:uint = sourceBM.width;
			var height:uint = sourceBM.height;
			var number1:int = source.name.indexOf(".");
			
			var savePosition:File= targetFolder.resolvePath(source.name.substring(0,number1)+".bmd");
			imageSaver.saveTxt(sourceBM,new Rectangle(0,0,width,height),true,savePosition,true);
			this.progress(100);
		}
		
		private function handlePngSaved(e:Event):void
		{
			if(stop)
			{
				return;
			}
			trace("数据保存成功");
			this.text="数据保存成功";
			this.dispatchEvent(new MyEvent("fileOk",this.index));
			sourceBM.bitmapData.dispose();
	     	imageSaver.removeEventListener("imageSaved",handlePngSaved);
		}
		
	}
}